﻿@{
    ViewBag.Title = "Manage Users";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@if (User != null && DoeLibsMVC.Security.SecurityHelper.currentUserIsInStaffGroup())
{
    <div id="PresentationBox">
        <h2 class="centeredHeaderText">Search for users</h2>

		<div id="searchBarOnContentPage">
			<form id="frmSearch">
				<input type="text" placeholder="search..." class="searchFormTxtBox" id="txtSearch" />
				<input type="submit" value="search" class="generalBtn" />
			</form>
		</div>

		<table class="DataPresentationtable" id="resultTable">
		</table>
    </div>
	
    <script type="text/javascript">

		$(document).ready(function () {
			$('#frmSearch').submit(function (e) {
				onSearch();
				e.preventDefault();
			});
		});
		
		function onSearch()
		{
			var txtSearch = document.getElementById('txtSearch');
			var searchTerm = txtSearch.value;
			var table = document.getElementById('resultTable');
			table.innerHTML = '<tr><td><center><img src="/Content/images/loading.gif" />loading data...</center></td></tr>';
			$.ajax({
				url: "/api/user/?searchTerm=" + searchTerm,
				type: "GET",
				beforeSend: function (xhr) {
					xhr.setRequestHeader("Authorization", getAuthorizationHeader('@User.UserId', ''));
				}
			}).done(function (data) {
				insertDataIntoAnchor(table, data);
				txtSearch.blur();
			}).fail(function () {
				table.innerHTML = '<tr><td><center>No users where found ...</center></td></tr>'
			});
		}

		function insertDataIntoAnchor(anchor, data)
		{
			anchor.innerHTML = '';
			var innerHTML = '';

			innerHTML = '<thead><tr><th>Name</th><th>Email</th><th>Category</th><th>Expiration date</th><th style="width: 30%;text-align: center;">Action</th></tr></thead>';
			innerHTML += '<tbody>';
			for (var i = 0; i < data.length; i++)
			{
				innerHTML += '<tr>';
				innerHTML += '<td>' + data[i].FirstName + ' ' + data[i].LastName + '</td>';
				innerHTML += '<td>' + data[i].EMail + '</td>';
				innerHTML += '<td>' + data[i].Category.Name + '</td>';
				if (data[i].UserExpirationDate)
					innerHTML += '<td>' + parseDateTimeToDate(data[i].UserExpirationDate) + '</td>';
				else
					innerHTML += '<td>' + 'Never' + '</td>';
				if (data[i].Category.CategoryId == 1)
					innerHTML += '<td>' + '<input type="button" value="delete" class="checkInOutBtn" onclick="expireUser(' + data[i].UserId + ')" />' + '</td>';
				else
					innerHTML += '<td>' + '<input type="button" value="renew expiration date" class="checkInOutBtn" onclick="renewUser(' + data[i].UserId + ')" />' + '</td>';
				innerHTML += '</tr>';
			}
			innerHTML += '</tbody>';

			anchor.innerHTML = innerHTML;
		}

		function parseDateTimeToDate(dateTime)
		{
			return dateTime.substring(0, dateTime.indexOf('T'));
		}

		function renewUser(userId) {

			var confirm = window.confirm("Do you really want to renew this user?");
			if (confirm) {
				$.ajax({
					url: "/api/user/" + userId,
					type: "PUT",
					beforeSend: function (xhr) {
						xhr.setRequestHeader("Authorization", getAuthorizationHeader('@User.UserId', ''));
					}
				}).done(function () {
					console.log("users expire date has been renewed (" + userId + ")");
					setHttpMessageDynamic("Users expire date has been renewed", true);
					onSearch();
					window.scrollTo(0, 0);
				}).fail(function () {
					console.log("user could not be renewed (" + userId + ")");
					setHttpMessageDynamic("Technical problems, please try again", false);
					onSearch();
					window.scrollTo(0, 0);
				});
			}
		}

		function expireUser(userId) {

			document.location.href = '/User/UserDelete/' + userId;
			return;
			var confirm = window.confirm("Do you really want to delete this user?\nThe users loans and reservations will be recalled and his loanables will be made unavailable.");
			if (confirm) {
				$.ajax({
					url: "/api/user/" + userId,
					type: "DELETE",
					beforeSend: function (xhr) {
						xhr.setRequestHeader("Authorization", getAuthorizationHeader('@User.UserId', ''));
					}
				}).done(function () {
					console.log("user has been marked to expire (" + userId + ")");
					setHttpMessage("User has been set to termination", true);
					onSearch();
					window.scrollTo(0, 0);
				}).fail(function () {
					console.log("user could not be marked to expire (" + userId + ")");
					setHttpMessage("Technical problems, please try again", false);
					onSearch();
					window.scrollTo(0, 0);
				});
			}
		}
    </script>
}
